package com.famdoc.Service.Impl;

import com.famdoc.Mapper.VisitMapper;
import com.famdoc.Pojo.dto.Result;
import com.famdoc.Pojo.dto.VisitFrom;
import com.famdoc.Pojo.entity.CheckBody;
import com.famdoc.Pojo.entity.CheckItem;
import com.famdoc.Pojo.entity.Patient;
import com.famdoc.Pojo.entity.Visit;
import com.famdoc.Pojo.vo.VisitCheckItem;
import com.famdoc.Pojo.vo.VisitPageBean;
import com.famdoc.Pojo.vo.VisitVO;
import com.famdoc.Service.VisitService;
import com.famdoc.Utils.SendMQUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class VisitServiceImpl implements VisitService {

    @Autowired
    private VisitMapper visitMapper;

    @Autowired
    private SendMQUtils sendMQUtils;

    /**
     * 新增随访
     * @param visitFrom
     * @return
     */
    @Override
    public Result addVisit(VisitFrom visitFrom) {
        CheckItem checkItem = new CheckItem(visitFrom);
        //先插入体检项目
        int checkItemId = visitMapper.addCheckItem(checkItem);
        if(checkItemId>0){
            CheckBody checkBody = new CheckBody(visitFrom);
            checkBody.setCheckItemId(checkItem.getId());
            //再插入体检记录
            int checkId = visitMapper.addCheck(checkBody);
            if(checkId>0){
                Visit visit = new Visit(visitFrom);
                visit.setCheckId(checkBody.getId());
                //最后插入随访消息
                int num = visitMapper.addVisit(visit);
                if(num>0){
                    //如果为预约随访
                    if(visit.getVisitSign()==-1){
                        //发送延时消息
                        sendMQUtils.sendVisitMQ(visit.getId(), visit.getVisitTime());
                    }
                    return Result.success(getVisitVO(visit));
                }
            }
            return Result.error(201,"新增随访失败！");
        }
        return Result.error(201,"新增随访失败！");
    }

    /**
     * 获取随访数据类
     * @param visit
     * @return
     */
    public VisitVO getVisitVO(Visit visit){
        System.out.println(visit);
        Patient patient = visitMapper.getPatient(visit.getPatientId());
        VisitVO visitVO =new VisitVO();
        visitVO.setId(visit.getId());
        visitVO.setName(patient.getName());
        visitVO.setAge(patient.getAge());
        visitVO.setSex(patient.getSex());
        visitVO.setDisease(patient.getDisease());
        visitVO.setDoctorId(patient.getDoctorId());
        visitVO.setPatientId(patient.getId());
        visitVO.setCheckId(visit.getCheckId());
        visitVO.setSymptoms(visit.getSymptoms());
        visitVO.setDrugUsage(visit.getDrugUsage());
        visitVO.setVisitTime(visit.getVisitTime());
        visitVO.setVisitWay(visit.getVisitWay());
        visitVO.setLivingHabit(visit.getLivingHabit());
        visitVO.setVisitSign(visit.getVisitSign());
        visitVO.setRemark(visit.getRemark());
        System.out.println(visitVO);
        return visitVO;
    }

    /**
     * 通过患者姓名/身份证/电话，搜索随访列表
     *
     * @param keyword
     * @param doctorId
     * @return
     */
    @Override
    public Result searchVisitByKeyword(String keyword, Integer doctorId) {
        List<VisitVO> visitList = visitMapper.searchVisitByKeyword(keyword,doctorId);
        //获取草稿数
        int draftNum = visitMapper.getDraftNum(doctorId);
        //获取完成的随访数
        int completedNum = visitMapper.getCompletedNum(doctorId);
        //封装随访数据类
        VisitPageBean visitPageBean = new VisitPageBean(draftNum,completedNum,visitList);
        return Result.success(visitPageBean);
    }

    /**
     * 获取随访列表（没有任何筛选）
     *
     * @param doctorId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Result getVisitList(Integer doctorId, Integer page, Integer pageSize) {
        //计算开始下标
        int start = (page-1)*pageSize;
        List<VisitVO> visitList = visitMapper.getVisitList(doctorId,start,pageSize);
        //获取草稿数
        int draftNum = visitMapper.getDraftNum(doctorId);
        //获取完成的随访数
        int completedNum = visitMapper.getCompletedNum(doctorId);
        //封装随访数据类
        VisitPageBean visitPageBean = new VisitPageBean(draftNum,completedNum,visitList);
        return Result.success(visitPageBean);
    }

    /**
     *  获取随访列表（通过日期筛选，0代表全部，3代表最近三个月，6代表最近半年，12代表最近一年）
     * @param type
     * @return
     */
    @Override
    public Result flierVisitByDate(Integer doctorId,Integer type) {
        List<VisitVO> visitList =new ArrayList<>();
        //获取当前时间
        LocalDateTime endDate = LocalDateTime.now();
        //获取开始的时间
        LocalDateTime startDate = endDate.minusMonths(type);
        visitList = visitMapper.flierVisitByDate(doctorId,startDate,
                endDate);
        //获取草稿数
        int draftNum = visitMapper.getDraftNum(doctorId);
        //获取完成的随访数
        int completedNum = visitMapper.getCompletedNum(doctorId);
        //封装随访数据类
        VisitPageBean visitPageBean = new VisitPageBean(draftNum,completedNum,visitList);
        return Result.success(visitPageBean);
    }

    /**
     * 修改随访
     * @param visitFrom
     * @return
     */
    @Override
    public Result updateVisit(VisitFrom visitFrom) {
        System.out.println(visitFrom);
        CheckItem checkItem = new CheckItem(visitFrom);
        checkItem.setId(visitFrom.getCheckItemId());
        //修改随访体检项目数据
        int num1 = visitMapper.updateCheckItem(checkItem);
        if(num1>0){
            CheckBody checkBody = new CheckBody(visitFrom);
            checkBody.setId(visitFrom.getCheckId());
            //修改随访体检记录数据
            int num2 = visitMapper.updateCheck(checkBody);
            if(num2>0){
                Visit visit = new Visit(visitFrom);
                //修改随访信息数据
                int num3 = visitMapper.updateVisit(visit);
                if(num3>0){
                    return Result.success(getVisitVO(visit));
                }
            }
            return Result.error(201,"修改随访失败！");
        }
        return Result.error(201,"修改随访失败！");
    }

    /**
     * 删除随访
     *
     * @param id
     * @param checkId
     * @param checkItemId
     * @return
     */
    @Override
    public Result deleteVisit(Integer id, Integer checkId, Integer checkItemId) {
        //先删除随访体检记录
        int num1 = visitMapper.deleteCheck(checkId);
        if(num1>0){
            //再删除体检记录相关的体检项目
            int num2 = visitMapper.deleteCheckItem(checkItemId);
            if(num2>0){
                //最后删除随访记录
                int num3 = visitMapper.deleteVisit(id);
                if(num3>0){
                    return Result.success("随访删除成功！");
                }
            }
            return Result.error(201,"随访删除失败！");
        }
        return Result.error(201,"随访删除失败！");
    }

    /**
     * 获取签约患者列表
     * @param doctorId
     * @param keyword
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Result getSignPatient(Integer doctorId, String keyword, Integer page, Integer pageSize) {
        //计算开始下标
        int start = (page-1)*pageSize;
        List<Patient> patientList = visitMapper.getSignPatient(doctorId,keyword,start,pageSize);
        return Result.success(patientList);
    }

    /**
     * 获取随访记录对应的体检记录
     * @param checkId
     * @return
     */
    @Override
    public Result getVisitCheckItem(Integer checkId) {
        VisitCheckItem visitCheckItem = visitMapper.getVisitCheckItem(checkId);
        return Result.success(visitCheckItem);
    }

    /**
     * 获取病人对应的体检记录列表
     * @param patientId
     * @return
     */
    @Override
    public Result getCheckItemList(Integer patientId) {
        List<VisitCheckItem> visitCheckItemList = visitMapper.getCheckItemList(patientId);
        return Result.success(visitCheckItemList);
    }

    /**
     * 通过随访id查询随访
     * @param visitId
     * @return
     */
    @Override
    public Visit getVisitById(int visitId) {
        return visitMapper.getVisitById(visitId);
    }

    /**
     * 更新随访的状态为已逾期
     */
    @Override
    public void updateVisitStatusDue(int visitId) {
        visitMapper.updateVisitStatusDue(visitId);
    }
}
